import requests,xlrd,xlwt
# url_string = r'http://douban.uieee.com/v2/movie/in_theaters'
# response = requests.get(url_string)
# obj = response.json()
# move_list = obj['subjects']
# list_all=[]
# for item in move_list:
#     list_name = []
#     for m in item["casts"]:
#         list_name.append(m['name'])
#     list_all.append([item['title'],item['genres'],item["rating"]["average"],list_name])
# print(list_all)
# wbt = xlwt.Workbook()
# sheet = wbt.add_sheet('电影')
# title=['电影名称','类型','评分','主演']
# data=list_all
# for i in range(len(title)):
#     sheet.write(0, i, title[i])
# for m in range(len(data)):
#     for n in range(len(data[m])):
#         sheet.write(m + 1, n, data[m][n])
# wbt.save('move.xls')

from urllib import request
import json
url_string = r'https://douban.uieee.com/v2/movie/top250'
# headers = {
#     'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64)',
#     'Host':'douban.uieee.com/v2/movie/top250'
# }
req = request.Request(url_string)
with request.urlopen(req) as f:
    data = f.read().decode('utf-8')
    data1 = json.loads(data)
move_list = data1['subjects']
list_all=[]
for item in move_list:
    list_name = []
    for m in item["casts"]:
        list_name.append(m['name'])
    list_all.append([item['title'],item['genres'],item["rating"]["average"],list_name])
for i in list_all:
    print(i)
wbt = xlwt.Workbook()
sheet = wbt.add_sheet('豆瓣TOP250')
title=['电影名称','类型','评分','主演']
data=list_all
for i in range(len(title)):
    sheet.write(0, i, title[i])
for m in range(len(data)):
    for n in range(len(data[m])):
        sheet.write(m + 1, n, data[m][n])
wbt.save('豆瓣.xls')




